//
//  CommentViewController.m
//  DialectShare
//
//  Created by timmy zhong on 4/1/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "CommentViewController.h"
#import "Comment.h"
#import "CommentDB.h"
#import "CommentCell.h"
#import <QuartzCore/QuartzCore.h>

#define GetComments     1
#define SendComments    2

@interface CommentViewController ()
@property (nonatomic, retain) DialectProxy *proxy;
@property (nonatomic, retain) NSMutableArray *comments;
@end

@implementation CommentViewController
@synthesize tableView = _tableView, proxy = _proxy, comments = _comments;
@synthesize dialect = _dialect, input = _input;

- (void)dealloc
{
    self.tableView = nil;
    self.proxy = nil;
    self.dialect = nil;
    self.comments = nil;
    self.input = nil;
    [super dealloc];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.input.layer.cornerRadius = 10;
    self.input.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.input.layer.borderWidth = 1.0f;
    
    _comments = [[NSMutableArray alloc] initWithCapacity:20];
    _proxy = [[DialectProxy alloc] initWithDelegate:self];
    _proxy.object = [NSNumber numberWithInt:GetComments];
    [_proxy commentsOfDialect:self.dialect];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)back:(id)sender
{
    [self dismissModalViewControllerAnimated:YES];
}

#pragma mark - proxy delegate
- (void)requestFailWithProxy:(Proxy *)proxy
{
    
}

- (void)requestFinishWithProxy:(Proxy *)proxy
{
    if ([proxy.object isKindOfClass:[NSNumber class]]) {

        NSInteger tag = [(NSNumber *)proxy.object intValue];
        if (tag == GetComments) {
            CommentDB *db = [[CommentDB alloc] init];
            [self.comments removeAllObjects];
            [self.comments addObjectsFromArray:[db commentsOfDialect:self.dialect]];
            [self.tableView reloadData];
        }
    } else if ([proxy.object isKindOfClass:[Comment class]]) {
        
        Comment *comment = (Comment *)proxy.object;
        [self.comments insertObject:comment atIndex:0];
        [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
    }
    
}

#pragma mark - table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.comments count];
}

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[CommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    }
    Comment *comment = [self.comments objectAtIndex:indexPath.row];
    [((CommentCell *)cell) showComment:comment];
    return cell;
}

- (IBAction)comment:(id)sender
{
    NSString *content = self.input.text;
    if ([content isEqualToString:@""]) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"评论不能为空" delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        return ;
    }
    Comment *comment = [[Comment alloc] init];
    comment.content = content;
    comment.dialectId = self.dialect.dialectId;
    _proxy.object = [NSNumber numberWithInt:SendComments];
    [_proxy sendComment:comment];
    [comment release];
}

- (IBAction)cancel:(id)sender
{
    [self.input resignFirstResponder];
}













@end
